#pragma once
#include "AbstractThread.h"
#include "KernelEvent.h"

namespace WTB{

	class CTimer;

	class CTimerThread : public CAbstractThread{

	public:
		CTimerThread():
		  m_pTimer(NULL),
			  m_pKernelEvent(NULL)
		  {

		  }

		  int Create(CTimer* _pEventTimer,CKernelEvent* _pKernelEvent)
		  {
			  CAbstractThread::Create();
			  m_pTimer = _pEventTimer;
			  m_pKernelEvent = _pKernelEvent;
			  return 0;
		  }

		  void Close()
		  {
			  m_pTimer = NULL;
			  m_pKernelEvent = NULL;
			  CAbstractThread::Close();
		  }


	protected:

		unsigned int run();

	private:
		CTimer* m_pTimer;
		CKernelEvent* m_pKernelEvent;
	};

	class CTimerThreads{
	public:
		CTimerThreads():
		  m_nEventTimerThreadAmount(0),
			  m_pArrEventTimerThreads(NULL),
			  m_nNextKernelEventIndex(0)
		  {

		  }

		  int Create(CTimer* _pEventTimer, int _nEventTimerThreadAmount);

		  void Close();

		  int SetKernelEvent()
		  {
			  m_pArrKernelEvents[m_nNextKernelEventIndex++].Set();
			  if (m_nNextKernelEventIndex == m_nEventTimerThreadAmount)
			  {
				  m_nNextKernelEventIndex = 0;
			  }

			  return 0;
		  }

	private:
		CKernelEvent* m_pArrKernelEvents;
		int m_nEventTimerThreadAmount;
		CTimerThread* m_pArrEventTimerThreads;

		int m_nNextKernelEventIndex;
	};
}